package com.jorado.etl.util;

import com.clearspring.analytics.stream.cardinality.AdaptiveCounting;
import com.clearspring.analytics.stream.cardinality.ICardinality;
import com.jorado.core.utility.StringUtils;

import java.io.*;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

public final class FileUtils {

    private FileUtils() {
    }

    public static void removeDuplicates(String filePath) {

        Runtime rt = Runtime.getRuntime();
        long start = System.currentTimeMillis();
        long updateRate = 1000000L;
        long count = 0L;
        ICardinality card = AdaptiveCounting.Builder.obyCount(Integer.MAX_VALUE).build();
        File file = new File(filePath);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString;
            while (StringUtils.isNotNullOrWhiteSpace(tempString = reader.readLine())) {
                card.offer(tempString);
                count++;
                if (updateRate > 0 && count % updateRate == 0) {
                    System.out.println("Total count:[" + count + "] Unique count:[" + card.cardinality() + "] FreeMemory:[" + rt.freeMemory() + "] ..");
                }
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("Total count:[" + count + "] Unique count:[" + card.cardinality() + "] FreeMemory:[" + rt.freeMemory() + "] ..");
        System.out.println("Total cost:[" + (end - start) + "] ms ..");
    }

    /**
     * 合并记录并去重
     *
     * @param lines
     * @return
     */
    public static List<String> margin(List<String>... lines) {
        List<String> result = new ArrayList<>();
        LinkedHashSet<String> set = new LinkedHashSet<>();
        for (List<String> list : lines) {
            set.addAll(list);
        }
        result.addAll(set);
        return result;
    }

    /**
     * 重命名文件
     *
     * @param from
     * @param to
     */
    public static void renameFile(String from, String to) {
        new File(from).renameTo(new File(to));
    }

    /**
     * 复制文件
     *
     * @param source
     * @param dest
     * @throws IOException
     */
    public static void copyFile(String source, String dest) throws IOException {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(new File(source)).getChannel();
            outputChannel = new FileOutputStream(new File(dest)).getChannel();
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } finally {
            inputChannel.close();
            outputChannel.close();
        }
    }
}

